/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.service.*;
import me.zhengjie.modules.system.domain.vo.CourseQueryCriteria;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
* @author shitou
* @date 2024-04-21
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "课程管理管理")
@RequestMapping("/api/course")
public class CourseController {

    private final CourseService courseService;

    private final CourseandclazzService courseandclazzService;

    private final CourseandteachService courseandteachService;

    private final ClazzService clazzService;

    private final TeachService teachService;

    private final ClazzandstudentService clazzandstudentService;

    private final CoursechapterService coursechapterService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('course:list')")
    public void exportCourse(HttpServletResponse response, CourseQueryCriteria criteria) throws IOException {
        courseService.download(courseService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询课程管理")
    @ApiOperation("查询课程管理")
//    @PreAuthorize("@el.check('course:list')")
    public ResponseEntity<PageResult<Course>> queryCourse(CourseQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(courseService.queryAllCourse(criteria,page),HttpStatus.OK);
    }

//    @GetMapping
//    public ResponseEntity<PageResult<Course>> getCourseList(CourseQueryCriteria criteria, Page<Object> page){
//
//        PageResult<Course> coursePageResult = courseService.queryAll(criteria,page);
//
////        coursePageResult.set
//        return new ResponseEntity<>(courseService.getCourseList(criteria,page),HttpStatus.OK);
//
//    }

    @GetMapping("/getTeacherCourseList")
    @Log("获取教师课程")
    @ApiOperation("获取教师课程")
//    @PreAuthorize("@el.check('course:list')")
    public ResponseEntity<List<Course>> getTeacherCourseList(CourseQueryCriteria criteria){
        return new ResponseEntity<>(courseService.getTeacherCourseList(criteria),HttpStatus.OK);
    }

    @GetMapping("/queryCourseMsg")
    @Log("查询课程详情")
    @ApiOperation("查询课程详情")
    public ResponseEntity<Object> queryCourseMsg(@RequestParam("courseid") Integer courseid){


        Course courseMsg = courseService.queryCourseMsg(courseid);
        List<Courseandclazz> courseandclazzMsg = courseandclazzService.queryCourseAndClazzMsg(courseid);
        List<Courseandteach> courseandteachMsg = courseandteachService.queryCourseAndTeachMsg(courseid);

        List<Long> teachIds = new ArrayList<>();
        List<Long> clazzIds = new ArrayList<>();

        for(Courseandclazz courseandclazz : courseandclazzMsg){
            clazzIds.add(courseandclazz.getClazzId());
        }

        for(Courseandteach courseandteach : courseandteachMsg){
            teachIds.add(courseandteach.getTeachid());
        }


        List<Teach> teachList = teachService.queryTeach(teachIds);
        List<Clazz> clazzList = clazzService.queryClazzList(clazzIds);
        for(Clazz clazz : clazzList){
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("clazzid",clazz.getId());
            clazz.setCount(clazzandstudentService.count(queryWrapper));
        }

        courseMsg.setTeachList(teachList);
        courseMsg.setClazzList(clazzList);


        return new ResponseEntity<>(courseMsg,HttpStatus.OK);
    }


    @PostMapping
    @Log("新增课程管理")
    @ApiOperation("新增课程管理")
    public ResponseEntity<Object> createCourse(@Validated @RequestBody Course resources){
        courseService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PostMapping("/createDeparmentCourse")
    @Log("新增部门课程管理")
    @ApiOperation("新增部门课程管理")
    public ResponseEntity<Object> createDeparmentCourse(@Validated @RequestBody Course resources){
        courseService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改课程管理")
    @ApiOperation("修改课程管理")
//    @PreAuthorize("@el.check('course:edit')")
    public ResponseEntity<Object> updateCourse(@Validated @RequestBody Course resources){
        courseService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除课程管理")
    @ApiOperation("删除课程管理")
//    @PreAuthorize("@el.check('course:del')")
    public ResponseEntity<Object> deleteCourse(@RequestBody List<Integer> ids) {
        courseService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}